#include"goods_list_op.h"
#include"value.h"
#include<stdio.h>
#include<stdlib.h>
/*初始化头节点*/
Goodslist * init_list(void)
{
    Goodslist *goods_l = malloc(sizeof(Goodslist));
    if(goods_l!=NULL)
    {
        goods_l->next = NULL;
    }
    return goods_l;
}


/*创建新节点*/
Goodslist * new_node(Goods data)
{
    Goodslist * new =malloc(sizeof(Goodslist));
    if(new!=NULL){
        new->id=data.id;
        strcpy(new->name,data.name);
        new->price = data.price;
        new->quantity = data.quantity;
        strcpy(new->type,data.type);
        new->next = NULL;
        goods_count++;
    }
    return new;
}

/*判断链表是否为空*/
bool is_empty(Goodslist *list){
    return list->next==NULL;
}

/*插入节点*/
void insert_node(Goodslist * p,Goodslist * new)
{
    if(p==NULL || new ==NULL){
        return;
    }
    new->next=p->next;
    p->next=new;
}

/*删除节点*/
bool remove_node(Goodslist *mylist,Goodslist *delete)
{
    if(is_empty(mylist)){
        return false;
    }
    Goodslist * p=mylist;
    while(p!=NULL && p->next!=delete){
        p=p->next;
    }
    if(p==NULL){
        return false;
    }

    p->next=delete->next;
    delete->next=NULL;
    goods_count--;
    return true;
}

/*根据查找节点*/
Goodslist* find_node(Goodslist* mylist,int id)
{
    if(is_empty(mylist)){
        return NULL;
    }
    Goodslist* p;
    // while(p!=NULL&&p->data!=data){
    //     p=p->next;
    // }
    for(p=mylist->next;p!=NULL;p=p->next){
        if(p->id==id){
            break;
        }
    }
    return p;
}

/*根据name查找节点*/
Goodslist* find_node_by_name(Goodslist* mylist,char *name)
{
    if(is_empty(mylist)){
        return NULL;
    }
    Goodslist* p;
    // while(p!=NULL&&p->data!=data){
    //     p=p->next;
    // }
    for(p=mylist->next;p!=NULL;p=p->next){
        if(strcmp(p->name,name) == 0){
            break;
        }
    }
    return p;
}

/*根据type查找节点*/
Goodslist* find_node_by_type(Goodslist* mylist,char *type)
{
    if(is_empty(mylist)){
        return NULL;
    }
    Goodslist* p;
    // while(p!=NULL&&p->data!=data){
    //     p=p->next;
    // }
    for(p=mylist->next;p!=NULL;p=p->next){
        if(strcmp(p->type,type) == 0){
            break;
        }
    }
    return p;
}

/*
** 功能：从文件加载数据
** filename		文件名字
*/ 
void file_load_data(Goodslist* head, const char *filename)
{
	FILE *fp = fopen(filename, "r");
	if(fp == NULL)
	{
		perror("file_load_data fail");
		return ;
	}
	// 从文件读取数据，加载到链表
	Goodslist tmp; 
    goods_count = 0;
	while( fread(&tmp, sizeof(Goodslist), 1, fp) == 1 )
	{
        Goods goods = {
            .id=tmp.id,
            .price = tmp.price,
            .quantity = tmp.quantity
        };
        strcpy(goods.name,tmp.name);
        strcpy(goods.type,tmp.type);

		Goodslist *new = new_node(goods);
		insert_node(head, new);
        goods_count++;
	}
	fclose(fp);
}

/*
** 功能：保存数据到文件
** filename		文件名字
*/ 
void file_save_data(Goodslist *head, const char *filename)
{
	FILE *fp = fopen(filename, "w");
	if(fp == NULL)
	{
		perror("file_save_data fail");
		return ;
	}
	
	// 把链表数据遍历，写入文件
	Goodslist *tmp = head->next;
	
	for( ; tmp != NULL; tmp = tmp->next)
	{
		fwrite(tmp, sizeof(Goodslist), 1, fp);
	}	
	
	fclose(fp);
}

/*----根据Id排序---*/


